<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$table=pdo_get('jy_weishop_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if(!$table){
		exi('信息不存在！','error');
	}
	$group=pdo_get('jy_weishop_table_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table['group_id']));
	$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table['merchid']));
	$return['yuyue_typetitle']=$this->yuyue_tables[$merch['yuyue_type']];
	$data=$this->model('model')->days($return['basic']['days']);
	$worker=pdo_get('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['worker_id']));
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));	
	$return['day']=$data[$_RQ['dayid']];
	$return['table']=$table;
	$return['group']=$group;
	$return['merch']=$merch;
	$post=array(
		'merchid'=>intval($table['merchid']),
		'timeid'=>intval($_RQ['timeid']),
		'dayname'=>date('m-d',time()),
		'dayid'=>intval($_RQ['dayid']),
		'fee'=>$group['fee'],
		'username'=>$member['nickname'],
		'mobile'=>$member['mobile'],
		'fileds'=>$group['fileds'],
		'total'=>1,
		'serve_type'=>'0'
	);
	$return['nowh']=intval(date('H'));
	$return['post']=$post;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$tableid = $postdata['tableid'];
	$data=$this->model('model')->days($return['basic']['days']);
	if(!$postdata['timeid']){
			exi('请选择预定时间','error');
	}
	if(!$postdata['username']){
			exi('请输入联系人姓名','error');
	}
	if(!$postdata['mobile']){
			exi('请输入联系人手机号码','error');
	}  
	if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  	 
			exi('请输入正确的手机号码','error');
	} 
	if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
	}
	//判断待支付订单及判断是否超过可预订次数
	$table=pdo_get('jy_weishop_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
	$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table['merchid']));
	$return['yuyue_typetitle']=$this->yuyue_tables[$merch['yuyue_type']];
	$postdata['day']=$data[$postdata['dayid']]['ymd'];
	$num=pdo_count('jy_weishop_table_order',array('timeid'=>$postdata['timeid'],'uniacid'=>$_SESSION['uniacid'],'day'=>$postdata['day'],'tableid'=>$postdata['tableid'],'status'=>array(1,2,4)));
	if($num){
		exi('该时间段的'.$return['yuyue_typetitle'].'预定，请重新选择预定时间段！','error');
	}
		$totalprice=$postdata['fee'];
		$selfcount=pdo_count('jy_weishop_table_order',array('tableid'=>$postdata['tableid'],'uniacid'=>$_SESSION['uniacid'],'day'=>$postdata['day']),'id');
		$daysnum=$serve_time[$postdata['timeid']]['times']['start'];
		$daysnum=str_replace(':','',$daysnum);
		$selfnumber=$daysnum.sprintf("%03d", $selfcount+1);
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'merchid'=>$postdata['merchid'],
			'tableid'=>$postdata['tableid'],
			'timeid'=>$postdata['timeid'],
			'day'=>$postdata['day'],
			'money'=>$totalprice,
			'pay'=>$totalprice,
			'total'=>$postdata['total'],
			'address'=>$postdata['address'],
			'username'=>$postdata['username'],
			'mobile'=>$postdata['mobile'],
			'ordersn'=>get_ordersn('jy_weishop_table_order','ordersn'),
			'remark'=>$postdata['remark'],
			'fileds'=>ijson_encode($postdata['fileds']),
			'selfnumber'=>$selfnumber,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_weishop_table_order',$insert);
		$id=pdo_insertid();
		if($totalprice>0){
			$params=array(
					'id'=>$id,
					'title'=>'预定'.$return['yuyue_typetitle'],
					'fee'=>$totalprice,
					'plugin'=>'jy_city',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_city/yuding/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_city/yudingorder/',
					'error_url'=>'jy_city/yudingorder/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_weishop_table_order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
					'pay_id'=>$payid,
					'order_id'=>$id,
			);
			exi($res);
		}else{
			$payresult=array('fee'=>0);
			$order=$insert;
			$order['id']=$id;
			$this->model('model')->payresult($order,$payresult);
			exi('预定成功！','','jy_city/yudingorder/');
		}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_weishop_table_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$this->model('model')->payresult($order,$payresult);
	exi('支付成功','success','jy_city/yudingorder/');
}
if($_RQ['op']=='out_time'){
	$merchid=intval($_RQ['merchid']);
	$post['out_pay']=$_RQ['out_pay'];
	$out_time = model('jy_city/model')->out_times($merchid,$post);
	$timestamp = TIMESTAMP + 60 * 30;
	$text_time = "当前时间";
	$return['timestamp']=$timestamp;
	$return['out_time']=$out_time;
	exi($return);
}